//
//  FontSelectorView.m
//  ShareBoard
//
//  Created by lidi on 2018/6/12.
//  Copyright © 2018年 曹雪莹. All rights reserved.
//

#import "FontSelectorView.h"
#import <Masonry/Masonry.h>
#import "FontButton.h"

#define ANIMATE_DURATION    0.25f

@interface FontSelectorView()
@property (nonatomic,copy) void(^handle)(CGFloat font);
@property(nonatomic,strong) NSArray<NSString *> *fontArray;
@property(nonatomic,strong) NSArray<NSNumber *> *fontSizeArray;
@property (nonatomic, strong) UIView   *bgView;
@property (nonatomic, strong) UIView   *topSheetView;
@property (nonatomic, strong) NSArray<FontButton *>  *items;
@end

@implementation FontSelectorView

- (NSNumber *)fontSize {
    CGFloat fontSize = [[NSUserDefaults standardUserDefaults] integerForKey:kFontSize];
    NSInteger index = [self.fontSizeArray indexOfObject:@(fontSize)];
    if (index != NSNotFound) {
        return @(fontSize);
    } else {
        return @100;
    }
}

- (void)refreshFontButtons {
    for (NSInteger index = 0; index < self.items.count; index++) {
        FontButton *item = [self.items objectAtIndex:index];
        NSNumber *size = [self.fontSizeArray objectAtIndex:index];
        if (size.floatValue == [self fontSize].floatValue) {
            item.selected = YES;
        } else {
            item.selected = NO;
        }
    }
}

- (instancetype)initWithHandle:(void(^)(CGFloat font))handle {
    self = [super init];
    if (self) {
        self.fontArray = @[@"小",@"中",@"大",@"超大",@"巨大"];
        self.fontSizeArray = @[@50,@100,@150,@200,@250];
        //    背景，带灰度
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        //    加载分享面板
        [self loadUIConfig];
        self.handle = handle;
    }
    return self;
}

/**
 加载自定义视图，按钮的tag依次为（200 + i）
 */
- (void)loadUIConfig {
    
    _topSheetView = [UIView new];
    _topSheetView.backgroundColor = [UIColor blackColor];
    _topSheetView.alpha = 0.3;
    [self addSubview:_topSheetView];
    [_topSheetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(_topSheetView.superview);
    }];
    
    _bgView = [UIView new];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_bgView.superview);
        make.height.equalTo(@100.0f);
        make.bottom.equalTo(_bgView.superview).offset(100.0f);
    }];    

    //    可点击
    _topSheetView.userInteractionEnabled = YES;
    //    点击背景，收起底部分享面板，移除本视图
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedCancel)];
    [_topSheetView addGestureRecognizer:tapGesture];
    
    //    按钮
    UIView *pre = nil;
    NSMutableArray *fontButtons = [NSMutableArray array];
    for (NSInteger i = 0; i < self.fontArray.count; i++) {
        FontButton *buttonFont = [[FontButton alloc] initWith:i title:self.fontArray[i]];
        buttonFont.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(itemTap:)];
        [buttonFont addGestureRecognizer:tap];
        buttonFont.userInteractionEnabled = YES;
        [_bgView addSubview:buttonFont];
        [buttonFont mas_makeConstraints:^(MASConstraintMaker *make) {
            if (pre) {
                make.left.equalTo(pre.mas_right);
                make.width.equalTo(pre.mas_width);
            } else {
                make.left.equalTo(buttonFont.superview);
            }
            if (i == self.fontArray.count - 1) {
                make.right.equalTo(buttonFont.superview);
            }
            make.top.bottom.equalTo(buttonFont.superview);
        }];
        pre = buttonFont;
        [fontButtons addObject:buttonFont];
    }
    self.items = fontButtons;
    
    [self refreshFontButtons];
    
    [self layoutIfNeeded];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_bgView.superview);
        }];
        
        //    弹出
        [UIView animateWithDuration:ANIMATE_DURATION animations:^{
            [self layoutIfNeeded];
        }];
    });
}

#pragma mark --------------------------- Selector
- (void)itemTap:(UIGestureRecognizer *)ges {
    NSNumber *font = [self.fontSizeArray objectAtIndex:ges.view.tag];
    [[NSUserDefaults standardUserDefaults] setInteger:font.floatValue forKey:kFontSize];
    [self refreshFontButtons];
    if (self.handle) {
        self.handle(font.floatValue);
    }
}

/**
 点击取消
 */
- (void)tappedCancel {
    
    [UIView animateWithDuration:ANIMATE_DURATION animations:^{
        [self.bgView setFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, 0)];
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
        }
    }];
}

@end
